package com.nc.mem.serial.graph.common;

import static com.nc.mem.serial.graph.Representation.readInt;
import static com.nc.mem.serial.graph.Representation.writeInt;

import java.nio.ByteBuffer;

import com.nc.mem.serial.graph.GraphSerializer;


public final class EnumSerializer extends GraphSerializer {

	public static EnumSerializer INSTANCE = new EnumSerializer();

	private EnumSerializer() {
	}

	@Override
	public void doRead(final ByteBuffer src, final Object obj) {

	}

	@Override
	public void doWrite(final ByteBuffer dest, final Object obj) {
		StringSerializer.INSTANCE.writeNonRoot(dest, obj.getClass().getName());
		writeInt(dest, ((Enum<?>) obj).ordinal());
	}

	@Override
	public Object instantiate(final ByteBuffer src) {
		try {
			return Class.forName((String) StringSerializer.INSTANCE.readNonRoot(src)).getEnumConstants()[readInt(src)];
		} catch (final ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

}